package com.axx.android.utils;

import android.content.Context;
import android.text.TextUtils;

import com.axx.android.profile.auth.model.AuthUser;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;

/**
 * Created by sunjie on 2017/8/16.
 */

public class ContextUtils {
    public static void saveUser(Context context, AuthUser authUser){
        String string = GsonUtils.toJson(authUser);
        context.getSharedPreferences("user_info",0).edit().putString("user",string).commit();
    }

    public static AuthUser getUser(Context context){
        String string = context.getSharedPreferences("user_info",0).getString("user","");
        if (TextUtils.isEmpty(string)){
            return null;
        }else{
            try {
                AuthUser authUser = GsonUtils.fromJson(string,new TypeToken<AuthUser>(){});
                return authUser;
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static void clearUser(Context context) {
        context.getSharedPreferences("user_info",0).edit().clear().commit();
    }
}
